#import "UIImage+PPPExt.h"
@implementation UIImage (PPPExt)
- (UIImage *)resizedImage:(CGSize)newSize transform:(CGAffineTransform)transform interpolationQuality:(CGInterpolationQuality)quality {
	CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
	CGImageRef imageRef = self.CGImage;
	CGContextRef bitmap = CGBitmapContextCreate(NULL,
												newRect.size.width,
												newRect.size.height,
												8,
												0,
												CGImageGetColorSpace(imageRef),
												kCGImageAlphaNoneSkipLast);
	CGContextConcatCTM(bitmap, transform);
	CGContextSetInterpolationQuality(bitmap, quality);
	CGContextDrawImage(bitmap, newRect, imageRef);
	CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
	UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
	CGContextRelease(bitmap);
	CGImageRelease(newImageRef);
	return newImage;
}
- (CGAffineTransform)transformForOrientation:(CGSize)newSize {
	CGAffineTransform transform = CGAffineTransformIdentity;
	switch(self.imageOrientation) {
		case UIImageOrientationDown:           
		case UIImageOrientationDownMirrored:   
			transform = CGAffineTransformTranslate(transform, newSize.width, newSize.height);
			transform = CGAffineTransformRotate(transform, M_PI);
			break;
		case UIImageOrientationLeft:           
		case UIImageOrientationLeftMirrored:   
			transform = CGAffineTransformTranslate(transform, newSize.width, 0);
			transform = CGAffineTransformRotate(transform, M_PI_2);
			break;
		case UIImageOrientationRight:          
		case UIImageOrientationRightMirrored:  
			transform = CGAffineTransformTranslate(transform, 0, newSize.height);
			transform = CGAffineTransformRotate(transform, -M_PI_2);
			break;
		default:
			break;
	}
	switch(self.imageOrientation) {
		case UIImageOrientationUpMirrored:     
		case UIImageOrientationDownMirrored:   
			transform = CGAffineTransformTranslate(transform, newSize.width, 0);
			transform = CGAffineTransformScale(transform, -1, 1);
			break;
		case UIImageOrientationLeftMirrored:   
		case UIImageOrientationRightMirrored:  
			transform = CGAffineTransformTranslate(transform, newSize.height, 0);
			transform = CGAffineTransformScale(transform, -1, 1);
			break;
		default:
			break;
	}
	return transform;
}
- (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
	CGAffineTransform transform = CGAffineTransformIdentity;
	transform = [self transformForOrientation:newSize];
	return [self resizedImage:newSize transform:transform interpolationQuality:quality];
}
- (UIImage *)resizedImageWithContentMode:(UIViewContentMode)contentMode size:(CGSize)size interpolationQuality:(CGInterpolationQuality)quality {
	CGFloat horizontalRatio = size.width / self.size.width;
	CGFloat verticalRatio = size.height / self.size.height;
	CGFloat ratio;
	switch(contentMode) {
		case UIViewContentModeScaleAspectFill:
			ratio = MAX(horizontalRatio, verticalRatio);
			break;
		case UIViewContentModeScaleAspectFit:
			ratio = MIN(horizontalRatio, verticalRatio);
			break;
		default:
			[NSException raise:NSInvalidArgumentException format:@"Unsupported content mode: %ld", (long)contentMode];
	}
	CGSize newSize = CGSizeMake(self.size.width * ratio, self.size.height * ratio);
	return [self resizedImage:newSize interpolationQuality:quality];
}
- (UIImage *)croppedImage:(CGRect)bounds {
	CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
	UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
	CGImageRelease(imageRef);
	return croppedImage;
}
#pragma mark - fix orientation
- (UIImage *)fixOrientation {
	if (self.imageOrientation == UIImageOrientationUp) return self;
	CGAffineTransform transform = CGAffineTransformIdentity;
	switch (self.imageOrientation) {
		case UIImageOrientationDown:
		case UIImageOrientationDownMirrored:
			transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
			transform = CGAffineTransformRotate(transform, M_PI);
			break;
		case UIImageOrientationLeft:
		case UIImageOrientationLeftMirrored:
			transform = CGAffineTransformTranslate(transform, self.size.width, 0);
			transform = CGAffineTransformRotate(transform, M_PI_2);
			break;
		case UIImageOrientationRight:
		case UIImageOrientationRightMirrored:
			transform = CGAffineTransformTranslate(transform, 0, self.size.height);
			transform = CGAffineTransformRotate(transform, -M_PI_2);
			break;
		case UIImageOrientationUp:
		case UIImageOrientationUpMirrored:
			break;
	}
	switch (self.imageOrientation) {
		case UIImageOrientationUpMirrored:
		case UIImageOrientationDownMirrored:
			transform = CGAffineTransformTranslate(transform, self.size.width, 0);
			transform = CGAffineTransformScale(transform, -1, 1);
			break;
		case UIImageOrientationLeftMirrored:
		case UIImageOrientationRightMirrored:
			transform = CGAffineTransformTranslate(transform, self.size.height, 0);
			transform = CGAffineTransformScale(transform, -1, 1);
			break;
		case UIImageOrientationUp:
		case UIImageOrientationDown:
		case UIImageOrientationLeft:
		case UIImageOrientationRight:
			break;
	}
	CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
											 CGImageGetBitsPerComponent(self.CGImage), 0,
											 CGImageGetColorSpace(self.CGImage),
											 CGImageGetBitmapInfo(self.CGImage));
	CGContextConcatCTM(ctx, transform);
	switch (self.imageOrientation) {
		case UIImageOrientationLeft:
		case UIImageOrientationLeftMirrored:
		case UIImageOrientationRight:
		case UIImageOrientationRightMirrored:
			CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
			break;
		default:
			CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
			break;
	}
	CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
	UIImage *img = [UIImage imageWithCGImage:cgimg];
	CGContextRelease(ctx);
	CGImageRelease(cgimg);
	return img;
}
@end
